home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_gen / xv_pc16a.zip / X_PLOT.PAS < prev    next >
Pascal/Delphi Source File  |  1994-04-20  |  9KB  |  341 lines

  1. PROGRAM X_Plot;
  2. {
  3. Demonstration of how to plot a graph in a "canvas" in XView-PC.
  4. By Antonio Carlos Moreirao de Queiroz - acmq@coe.ufrj.br
  5. Compile with Turbo Pascal 7.0, using the units XVIEW.TPU e MICKEY.TPU.
  6. }
  7.  
  8. USES Graph,Crt,XView;
  9.  
  10. {Interface objects}
  11. VAR
  12.   MenuPrincipal,MenuGrafico:Xv_opaque;
  13.   fmensagens,tmsg:Xv_opaque;
  14.   fescalas,bplotar,tymax,tymin,txmax,txmin,tpontos:Xv_opaque;
  15.   fEscreverArquivo,tEscreverArquivo:Xv_opaque;
  16.   fLerArquivo,tLerArquivo:Xv_opaque;
  17.   fprincipal,cgrafico:Xv_opaque;
  18.  
  19. CONST
  20.   versao='1.1 - 20/04/94';
  21.   pontos0=400;                   {Maximum number of points in the graph}
  22.   xmin0=0;                       {Initial limits}
  23.   xmax0=50;
  24.   ymin0=-1;
  25.   ymax0=1;
  26.   grafico_valido:BOOLEAN=FALSE;  {If the graph is valid}
  27.   ultimo:INTEGER=0;              {Last computed point}
  28.  
  29. VAR
  30.   xmin,xmax,ymin,ymax:INTEGER;    {Limits in pixels of the graph}
  31.   cursor:INTEGER;                 {Cursor position in pixels}
  32.   ax,bx,ay,by,delta:REAL;         {Mapping constants}
  33.   gx,gy:ARRAY[1..pontos0] OF REAL;{X and Y values}
  34.   {It is not necessary to save X, but is simpler}
  35.  
  36. FUNCTION Sre(x:REAL; cm,dc:INTEGER):STRING;
  37. VAR
  38.   txt:STRING;
  39. BEGIN
  40.   Str(x:cm:dc,txt);
  41.   Sre:=txt
  42. END;
  43.  
  44. {$F+}
  45.  
  46. {Callbacks}
  47.  
  48. PROCEDURE EscreverArquivo(obj:Xv_opaque);
  49. BEGIN
  50.   {Notify handler for tEscreverArquivo}
  51.   ttysw_output(tmsg,'Would be written the file '+tEscreverArquivo^.panel_value+^M^J);
  52. END;
  53.  
  54. PROCEDURE LerArquivo(obj:Xv_opaque);
  55. BEGIN
  56.   {Notify handler for tLerArquivo}
  57.   ttysw_output(tmsg,'Would be read the file '+tLerArquivo^.panel_value+^M^J);
  58. END;
  59.  
  60. PROCEDURE DesenharGrafico(obj:Xv_opaque);
  61. VAR
  62.   i,xg,yg:INTEGER;
  63.   x,y:REAL;
  64. BEGIN
  65.   {Notify handler for cgrafico}
  66.   IF obj<>cgrafico THEN BEGIN
  67.     SetFillStyle(SolidFill,cgrafico^.back_color);
  68.     Bar(0,0,cgrafico^.dx,cgrafico^.dy);
  69.   END;
  70.   IF not grafico_valido THEN ultimo:=0;
  71.   {Eixos}
  72.   SetColor(cgrafico^.fore_color);
  73.   Line(60,10,60,cgrafico^.dy-12);
  74.   Line(60,cgrafico^.dy-12,cgrafico^.dx-10,cgrafico^.dy-12);
  75.   SetTextJustify(RightText,TopText);
  76.   OutTextXY(60,10,Sre(tymax^.panel_real,5,2));
  77.   SetTextJustify(RightText,BottomText);
  78.   OutTextXY(60,cgrafico^.dy-10,Sre(tymin^.panel_real,5,2));
  79.   SetTextJustify(RightText,TopText);
  80.   OutTextXY(cgrafico^.dx-10,cgrafico^.dy-10,Sre(txmax^.panel_real,5,2));
  81.   SetTextJustify(LeftText,TopText);
  82.   OutTextXY(60,cgrafico^.dy-10,Sre(txmin^.panel_real,5,2));
  83.   {Mapping}
  84.   xmin:=60;
  85.   xmax:=cgrafico^.dx-10;
  86.   ymin:=10;
  87.   ymax:=cgrafico^.dy-12;
  88.   ay:=(ymax-ymin)/(tymin^.panel_real-tymax^.panel_real);
  89.   by:=ymax-ay*tymin^.panel_real;
  90.   ax:=(xmax-xmin)/(txmax^.panel_real-txmin^.panel_real);
  91.   bx:=xmax-ax*txmax^.panel_real;
  92.   {Calculation and plotting}
  93.   cursor:=-10;
  94.   grafico_valido:=TRUE;
  95.   delta:=(txmax^.panel_real-txmin^.panel_real)/(tpontos^.panel_int-1);
  96.   x:=txmin^.panel_real;
  97.   FOR i:=1 TO tpontos^.panel_int DO BEGIN
  98.     IF i>ultimo THEN BEGIN {Only calculates if necessary}
  99.  
  100.       {----------------}
  101.  
  102.       {The plotted function is here}
  103.  
  104.       y:=exp(-x/10)*Sin(x);
  105.  
  106.       {----------------}
  107.  
  108.       gy[i]:=y;
  109.       gx[i]:=x;
  110.     END;
  111.     xg:=Round(ax*gx[i]+bx);
  112.     yg:=Round(ay*gy[i]+by);
  113.     IF i>1 THEN LineTo(xg,yg) ELSE MoveTo(xg,yg);
  114.     x:=x+delta;
  115.     IF KeyPressed THEN BEGIN
  116.       ultimo:=i;
  117.       Exit;
  118.     END;
  119.   END;
  120.   ultimo:=tpontos^.panel_int;
  121. END;
  122.  
  123. PROCEDURE TratarEventos(obj:Xv_opaque);
  124. VAR
  125.   i:INTEGER;
  126. BEGIN
  127.   {Event handler for cgrafico}
  128.   IF grafico_valido and (ie_shiftcode=1) THEN
  129.     {Finds the closest point}
  130.     i:=Round(1.0*(tpontos^.panel_int-1)*(ie_locx-xmin)/(xmax-xmin))+1;
  131.     IF (i>=1) and (i<=ultimo) THEN BEGIN
  132.       {Draws the cursor}
  133.       SetWriteMode(XorPut);
  134.       SetColor(c_white);
  135.       Line(cursor,ymin,cursor,ymax);
  136.       cursor:=Round(ax*gx[i]+bx);
  137.       Line(cursor,ymin,cursor,ymax);
  138.       SetWriteMode(NormalPut);
  139.       SetFillStyle(SolidFill,cgrafico^.back_color);
  140.       SetColor(cgrafico^.fore_color);
  141.       Bar3d(cgrafico^.dx-76,7,cgrafico^.dx-10,25,0,FALSE);
  142.       OutTextXY(cgrafico^.dx-74,9,'X:'+Sre(gx[i],5,2));
  143.       OutTextXY(cgrafico^.dx-74,17,'Y:'+Sre(gy[i],5,2));
  144.     END;
  145. END;
  146.  
  147. PROCEDURE TratarMenuPrincipal(obj:Xv_opaque);
  148. BEGIN
  149.   {Notify handler for MenuPrincipal}
  150.   CASE obj^.sel_menu OF
  151.     1:{Read data}
  152.       open_window(fLerArquivo);
  153.     2:{Write data}
  154.       open_window(fEscreverArquivo);
  155.     3:{Set scales}
  156.       open_window(fescalas);
  157.     4:{Messages}
  158.       open_window(fMensagens);
  159.     5:{Informations}
  160.       BEGIN
  161.         ttysw_output(tmsg,'Demostration: How to plot a graph in'+
  162.         ' linear scale using the XView-PC interface'^M^J);
  163.         ttysw_output(tmsg,'By Antonio Carlos Moreirao de Queiroz'^M^J);
  164.         ttysw_output(tmsg,'COPPE/EE/UFRJ - Version '+versao+^M^J);
  165.       END;
  166.     6:{Quit}
  167.       xv_end:=TRUE;
  168.   END;
  169. END;
  170.  
  171. PROCEDURE TratarMenuGrafico(obj:Xv_opaque);
  172. BEGIN
  173.   {Notify handler for MenuGrafico}
  174.   CASE obj^.sel_menu OF
  175.     1:{Set Scales}
  176.       open_window(fescalas);
  177.     2:{Main menu}
  178.       ttysw_output(tmsg,'Press the right button to access the submenu'^M^J);
  179.   END;
  180. END;
  181.  
  182. PROCEDURE Plotar(obj:Xv_opaque);
  183. BEGIN
  184.   {Notify handler for bplotar}
  185.   WHILE active_w<>fprincipal DO close_window(active_w);
  186.   DesenharGrafico(nil);
  187. END;
  188.  
  189. PROCEDURE InvalidarGrafico(obj:Xv_opaque);
  190. BEGIN
  191.   {Notify handler for txmax and txmin}
  192.   grafico_valido:=FALSE;
  193. END;
  194.  
  195. {$F-}
  196.  
  197. BEGIN
  198.   {Inicialization}
  199.   xv_init(0,0);
  200.   {Menus}
  201.   {Menu for fmensagens, fEscreverArquivo, fLerArquivo, and fprincipal}
  202.   MenuPrincipal:=xv_create(menu);
  203.   WITH MenuPrincipal^ DO BEGIN
  204.     xv_label:='Menu:';
  205.     item_create('read data');
  206.     item_create('write data');
  207.     item_create('set scales');
  208.     item_create('messages');
  209.     item_create('informations');
  210.     item_create('quit');
  211.     notify_handler:=TratarMenuPrincipal;
  212.   END;
  213.   {Menu for cgrafico}
  214.   MenuGrafico:=xv_create(menu);
  215.   WITH MenuGrafico^ DO BEGIN
  216.     xv_label:='Options:';
  217.     item_create('set scales');
  218.     item_create('main menu');
  219.     notify_handler:=TratarMenuGrafico;
  220.     item_submenu[2]:=MenuPrincipal;
  221.   END;
  222.   {Creation of the interface objects}
  223.   fescalas:=xv_create(frame);
  224.   WITH fescalas^ DO BEGIN
  225.     xv_label:='Scales';
  226.     x:=320;
  227.     y:=23;
  228.     dx:=250;
  229.     dy:=114;
  230.   END;
  231.   bplotar:=xv_create(button);
  232.   WITH bplotar^ DO BEGIN
  233.     xv_label:='Plot';
  234.     y:=75;
  235.     notify_handler:=Plotar;
  236.   END;
  237.   tpontos:=xv_create(textfield);
  238.   WITH tpontos^ DO BEGIN
  239.     xv_label:='points';
  240.     field_type:=int_field;
  241.     panel_int:=pontos0;
  242.     max_value:=pontos0;
  243.     min_value:=2;
  244.     y:=60;
  245.     notify_handler:=InvalidarGrafico;
  246.   END;
  247.   tymax:=xv_create(textfield);
  248.   WITH tymax^ DO BEGIN
  249.     xv_label:='y maximum';
  250.     y:=45;
  251.     field_type:=real_field;
  252.     panel_real:=ymax0;
  253.   END;
  254.   tymin:=xv_create(textfield);
  255.   WITH tymin^ DO BEGIN
  256.     xv_label:='y minimum';
  257.     y:=30;
  258.     field_type:=real_field;
  259.     panel_real:=ymin0;
  260.   END;
  261.   txmax:=xv_create(textfield);
  262.   WITH txmax^ DO BEGIN
  263.     xv_label:='x maximum';
  264.     y:=15;
  265.     field_type:=real_field;
  266.     panel_real:=xmax0;
  267.     notify_handler:=InvalidarGrafico;
  268.   END;
  269.   txmin:=xv_create(textfield);
  270.   WITH txmin^ DO BEGIN
  271.     xv_label:='x minimum';
  272.     field_type:=real_field;
  273.     panel_real:=xmin0;
  274.     notify_handler:=InvalidarGrafico;
  275.   END;
  276.   fmensagens:=xv_create(frame);
  277.   WITH fmensagens^ DO BEGIN
  278.     xv_label:='Messages';
  279.     x:=6;
  280.     y:=111;
  281.     dx:=319;
  282.     dy:=159;
  283.     menu_name:=MenuPrincipal;
  284.   END;
  285.   tmsg:=xv_create(tty);
  286.   fEscreverArquivo:=xv_create(frame);
  287.   WITH fEscreverArquivo^ DO BEGIN
  288.     xv_label:='Write File';
  289.     x:=6;
  290.     y:=67;
  291.     dx:=319;
  292.     dy:=43;
  293.     dymin:=27;
  294.     menu_name:=MenuPrincipal;
  295.   END;
  296.   tEscreverArquivo:=xv_create(textfield);
  297.   WITH tEscreverArquivo^ DO BEGIN
  298.     xv_label:='File';
  299.     value_length:=30;
  300.     notify_handler:=EscreverArquivo;
  301.     owner^.mouse_obj:=tEscreverArquivo;
  302.   END;
  303.   fLerArquivo:=xv_create(frame);
  304.   WITH fLerArquivo^ DO BEGIN
  305.     xv_label:='Read File';
  306.     x:=6;
  307.     y:=23;
  308.     dx:=319;
  309.     dy:=43;
  310.     dymin:=27;
  311.     menu_name:=MenuPrincipal;
  312.   END;
  313.   tLerArquivo:=xv_create(textfield);
  314.   WITH tLerArquivo^ DO BEGIN
  315.     xv_label:='File';
  316.     value_length:=30;
  317.     notify_handler:=LerArquivo;
  318.     owner^.mouse_obj:=tLerArquivo;
  319.   END;
  320.   fprincipal:=xv_create(frame);
  321.   WITH fprincipal^ DO BEGIN
  322.     xv_label:='X_Plot Demonstration Program';
  323.     dx:=GetMaxX;
  324.     dy:=GetMaxY;
  325.     menu_name:=MenuPrincipal;
  326.     adjust_exit:=FALSE;
  327.     mouse_obj:=fprincipal;
  328.   END;
  329.   cgrafico:=xv_create(canvas);
  330.   WITH cgrafico^ DO BEGIN
  331.     notify_handler:=DesenharGrafico;
  332.     event_handler:=TratarEventos;
  333.     menu_name:=MenuGrafico;
  334.     fore_color:=c_white;
  335.     back_color:=c_black;
  336.   END;
  337.   xv_main_loop(fprincipal);
  338.   {Fim}
  339.   TextMode(80);
  340. END.
  341.